<?php
	/* Functions in this file */
	/**************************/

	// GetClef()
	// GetMFCClassName()
	// GetMFCClassSchema()
	// GetPositionCount($voice)
	// GetStandardNotationStaffAboveSpacing()
	// GetStandardNotationStaffBelowSpacing()
	// GetSymbolSpacing()
	// GetTablatureStaffBelowSpacing()
	// GetTablatureStaffType()
	// IsValidClef($clef)
	// IsValidTablatureStaffType($type)
	// IsValidVoice($voice)
	// SetClef($clef)
	// SetStandardNotationStaffAboveSpacing($spacing)
	// SetStandardNotationStaffBelowSpacing($spacing)
	// SetSymbolSpacing($spacing)
	// SetTablatureStaffBelowSpacing($spacing)
	// SetTablatureStaffType($type)
?>
<?php
	 
	define('NUM_STAFF_VOICES', 2);
	 
	class Staff {
		 
		 
		// Default Constants
		const DEFAULT_DATA = 0x00000006;
		const DEFAULT_CLEF = 0;
		const DEFAULT_TABLATURE_STAFF_TYPE = 6;
		const DEFAULT_STANDARD_NOTATION_STAFF_ABOVE_SPACING = 9;
		const DEFAULT_STANDARD_NOTATION_STAFF_BELOW_SPACING = 9;
		const DEFAULT_SYMBOL_SPACING = 17;
		const DEFAULT_TABLATURE_STAFF_BELOW_SPACING = 0;
		 
		// Clef Constants
		const TREBLE_CLEF = 0;
		const BASS_CLEF = 1;
		 
		// Tablature $this->Staff Type Constants
		// Note: If these values change, update the Tuning constants for string count
		const MIN_TABLATURE_STAFF_TYPE = 3;
		const MAX_TABLATURE_STAFF_TYPE = 7;
		 
		 
		const clefMask = 0x000000f0;
		const tablatureStaffTypeMask = 0x0000000f;
		 
		 
		// Member Variables
		 
		protected $m_data = self::DEFAULT_DATA;
		protected $m_standardNotationStaffAboveSpacing = self::DEFAULT_STANDARD_NOTATION_STAFF_ABOVE_SPACING;
		protected $m_standardNotationStaffBelowSpacing = self::DEFAULT_STANDARD_NOTATION_STAFF_BELOW_SPACING;
		protected $m_symbolSpacing = self::DEFAULT_SYMBOL_SPACING;
		protected $m_tablatureStaffBelowSpacing = self::DEFAULT_TABLATURE_STAFF_BELOW_SPACING;
		 
		public $m_positionArray = array ();
		 
		 
		 
		 
		// MFC Class Functions
		 
		 
		function GetMFCClassName() {
			return "CStaff";
		}
		 
		 
		function GetMFCClassSchema() {
			return 1;
		}
		 
		 
		 
		 
		function IsValidClef($clef) {
			return (($clef >= self::TREBLE_CLEF) && ($clef <= self::BASS_CLEF));
		}
		 
		function GetClef() {
			return ((($this->m_data & self::clefMask) >> 4));
		}
		 
		 
		 
		 
		function IsValidTablatureStaffType($type) {
			return (($type >= self::MIN_TABLATURE_STAFF_TYPE) && (($type <= self::MAX_TABLATURE_STAFF_TYPE)));
		}
		 
		 
		function GetTablatureStaffType() {
			return (($this->m_data & self::tablatureStaffTypeMask));
		}
		 
		// Standard Notation $this->Staff Above Spacing Functions
		 
		 
		function SetStandardNotationStaffAboveSpacing($spacing) {
			$this->m_standardNotationStaffAboveSpacing = $spacing;
		}
		 
		 
		function GetStandardNotationStaffAboveSpacing() {
			return ($this->m_standardNotationStaffAboveSpacing);
		}
		 
		// Standard Notation $this->Staff Below Spacing Functions
		 
		 
		function SetStandardNotationStaffBelowSpacing($spacing) {
			$this->m_standardNotationStaffBelowSpacing = $spacing;
		}
		 
		 
		function GetStandardNotationStaffBelowSpacing() {
			return ($this->m_standardNotationStaffBelowSpacing);
		}
		 
		// Symbol Spacing Functions
		 
		 
		function SetSymbolSpacing($spacing) {
			$this->m_symbolSpacing = $spacing;
		}
		 
		 
		function GetSymbolSpacing() {
			return ($this->m_symbolSpacing);
		}
		 
		// Tablature $this->Staff Below Spacing Functions
		 
		 
		function SetTablatureStaffBelowSpacing($spacing) {
			$this->m_tablatureStaffBelowSpacing = $spacing;
		}
		 
		 
		function GetTablatureStaffBelowSpacing() {
			return ($this->m_tablatureStaffBelowSpacing);
		}
		 
		// Voice Functions
		 
		 
		 
		function IsValidVoice($voice) {
			return ($voice < NUM_STAFF_VOICES);
		}
		 
		// Position Functions
		 
		 
		 
		 
		function IsValidPositionIndex($voice, $index) {
			if (!$this->IsValidVoice($voice)) return 0;
			 return ($index < $this->GetPositionCount($voice));
		}
		 
		 
		 
		function GetPositionCount($voice) {
			if (!$this->IsValidVoice($voice)) return 0; 
			return sizeof($this->m_positionArray[$voice]);
		}
		 
		 
		 
		 
		function GetPosition($voice, $index) {
			if (!$this->IsValidPositionIndex($voice, $index)) 
				return NULL; 
			return ($this->m_positionArray[$voice][$index]);
		}
		 
		 
		 
		 
		 
		 
		 
		function Staff($tablatureStaffType = NULL, $clef = NULL) {
			 
			//------Last Checked------//
			// - Jan 5, 2005
			$this->SetClef($clef);
			$this->SetTablatureStaffType($tablatureStaffType);
		}
		 
		 
		// Serialize Functions
		 
		function DoSerialize(&$stream){
		 
		
			WRITE_BYTE ($stream,$this->m_data);
			WRITE_BYTE ($stream,$this->m_standardNotationStaffAboveSpacing);
			WRITE_BYTE ($stream,$this->m_standardNotationStaffBelowSpacing);
			WRITE_BYTE ($stream,$this->m_symbolSpacing);
			WRITE_BYTE ($stream,$this->m_tablatureStaffBelowSpacing);

			$arr = new PowerTabArray ();
			$arr->Serialize($stream,$this->m_positionArray[0]);
			
			$arr = new PowerTabArray ();
			$arr->Serialize($stream,$this->m_positionArray[1]);
			
			unset ($arr);

			return 1;
		}
		
		 
		 
		 
		 
		function DoDeserialize(&$stream){
		 
			$this->m_data = READ_BYTE ($stream);
			$this->m_standardNotationStaffAboveSpacing = READ_BYTE ($stream);
			$this->m_standardNotationStaffBelowSpacing = READ_BYTE ($stream);
			$this->m_symbolSpacing = READ_BYTE ($stream);
			$this->m_tablatureStaffBelowSpacing = READ_BYTE ($stream);
		
			$arr = new PowerTabArray ();
		 	$this->m_positionArray[0] = $arr->DeSerialize($stream , new Position());
		 	$arr = new PowerTabArray ();
			$this->m_positionArray[1] = $arr->DeSerialize($stream , new Position());
			
			unset ($arr);
		
		}
		
		 
		 
		 
		function SetClef($clef) {
			 
			//------Last Checked------//
			// - Jan 5, 2005
			if (!$clef <= 1) return 0;
			 
			$this->m_data &= ~self::clefMask;
			$this->m_data |= ($clef << 4);
			 
			return 1;
		}
		 
		 
		 
		 
		function SetTablatureStaffType($type) {
			 
			//------Last Checked------//
			// - Jan 5, 2005
			if (!Tuning::IsValidStringCount($type)) return 0;
			 
			$this->m_data &= ~self::tablatureStaffTypeMask;
			$this->m_data |= $type;
			 
			return 1;
		}

		// Modified by Ryan
		function GetClefName(){
			if($this->GetClef()){
				return "Bass";
			}else{
				return "Treble";
			}
		}
	}
	 
?>

